package cn.iocoder.yudao.module.mzt.controller.app.portal;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mzt.controller.admin.article.vo.ArticlePageReqVO;
import cn.iocoder.yudao.module.mzt.controller.admin.category.vo.CategoryRespVO;
import cn.iocoder.yudao.module.mzt.controller.app.portal.vo.*;
import cn.iocoder.yudao.module.mzt.dal.dataobject.article.ArticleDO;
import cn.iocoder.yudao.module.mzt.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.module.mzt.service.category.CategoryService;
import cn.iocoder.yudao.module.mzt.service.portal.PortalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.mzt.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS_ID;

@Tag(name = "用户 App - 首页数据")
@RestController
@RequestMapping("/mzt/portal")
@Validated
public class PortalController {
    @Resource
    private PortalService portalService;
    @Resource
    private CategoryService categoryService;
    /**
     * 获取系统配置
     *
     * @return
     */
    @Operation(summary = "获取系统配置")
    @GetMapping("/getSystemConfig")
    public CommonResult<List<PortalConfigRespVO>> getSystemConfig() {
        return success(portalService.getSystemConfig());
    }

    /**
     * 获取首页文章列表
     */
    @Operation(summary = "获取首页文章信息列表")
    @GetMapping("/getArticlePage")
    public CommonResult<PageResult<PortalArticleRespVO>> getArticlePage(@Valid ArticlePageReqVO pageReqVO) {
        PageResult<ArticleDO> pageResult = portalService.getArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PortalArticleRespVO.class));
    }
    @Operation(summary = "关键字搜索分类")
    @GetMapping("/inquireCategory")
    public CommonResult<List<CategoryRespVO>> inquireCategory() {
        List<CategoryDO> list = categoryService.getCategoryStatus("TOP");
        return success(BeanUtils.toBean(list, CategoryRespVO.class));
    }
    @Operation(summary = "根据根据关键字检索文章")
    @GetMapping("/inquireArticle")
    @Parameter(name = "keywords", description = "关键字", required = false)
    @Parameter(name = "categoryId", description = "分类id", required = false)
    public CommonResult<List<PortalArticleRespVO>> inquireArticle(@RequestParam(value = "categoryId", required = false) Long categoryId,@RequestParam(value = "tagId", required = false) Long tagId, @RequestParam(value = "keywords", required = false) String keywords) {
        List<ArticleDO> articleDOS = portalService.inquireArticle(categoryId, tagId,keywords);
        return success(BeanUtils.toBean(articleDOS, PortalArticleRespVO.class));
    }

    /**
     * 获取标签列表
     */
    @Operation(summary = "获取标签列表")
    @GetMapping("/getTagList")
    public CommonResult<List<PortalTagRespVO>> getTagList() {
        return success(portalService.getTagList());
    }

    /**
     * 获取推荐分类对应文章列表
     */
    @Operation(summary = "获取推荐分类对应文章列表")
    @GetMapping("/getRecommendArticleList")
    public CommonResult<List<PortalRecommendArticleRespVO>> getRecommendArticleList() {
        return success(portalService.getRecommendArticleList());
    }

    /**
     * 根据分类id获取分类信息
     */
    @Operation(summary = "根据分类id获取分类信息")
    @GetMapping("/getCategoryById")
    public CommonResult<PortalRecommendArticleRespVO> getCategoryById(@Valid PortalPageReqVO reqVO) {
        if (reqVO.getCategoryId() == null) {
            throw exception(CATEGORY_NOT_EXISTS_ID);
        }
        return success(portalService.getCategoryById(reqVO));
    }

    /**
     * 更具文章id获取文章信息
     */
    @Operation(summary = "根据id获取文章信息")
    @GetMapping("/getArticleById")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<PortalArticleRespVO> getArticleById(@RequestParam("id") Long id) {
        return success(portalService.getArticleById(id));
    }

    /**
     * 获取推荐文章、近期文章、排行榜文章、相关推荐
     */
    @Operation(summary = "获取推荐文章、最新文章、排行榜文章、相关推荐")
    @Parameter(name = "categoryId", description = "分类编号", required = true)
    @GetMapping("/getDataObject")
    public CommonResult<PortalDataObjectRespVO> getDataObject(@RequestParam("categoryId") Long categoryId) {
        return success(portalService.getDataObject(categoryId));
    }

    /**
     * 获取分类信息
     */
    @Operation(summary = "获取分类信息")
    @GetMapping("/getCategoryList")
    public CommonResult<PortalCategoryRespVO> getCategoryList() {
        return success(portalService.getCategoryList());
    }
}
